{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "44d78779-a4ae-4ad1-be13-f7a9aae648a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: openai in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (1.82.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (0.10.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (2.11.5)\n",
      "Requirement already satisfied: sniffio in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (4.67.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (4.13.2)\n",
      "Requirement already satisfied: idna>=2.8 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
      "Requirement already satisfied: certifi in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (2025.4.26)\n",
      "Requirement already satisfied: httpcore==1.* in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/app/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (0.4.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14dc02a3-4736-45f7-a2da-1978123fac95",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 从环境变量获取 DeepSeek API Key\n",
    "api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n",
    "if not api_key:\n",
    "    raise ValueError(\"请设置 DEEPSEEK_API_KEY 环境变量\") \n",
    "\n",
    "# 初始化 OpenAI 客户端（假设 DeepSeek 的 API 兼容 OpenAI 格式）\n",
    "client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API 的基地址\n",
    ")\n",
    "\n",
    "# 定义提示词\n",
    "prompt = \"\"\"请帮我用 HTML 生成一个五子棋游戏，所有代码都保存在一个 HTML 中。\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "428f7ead-142c-4f5c-a57b-d9194b51d725",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "五子棋游戏已保存为 gomoku.html\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    # 调用 DeepSeek Chat API\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"deepseek-chat\",  # 或 DeepSeek 提供的其他模型名称\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"你是一个专业的 Web 开发助手，擅长用 HTML/CSS/JavaScript 编写游戏。\"},\n",
    "            {\"role\": \"user\", \"content\": prompt}\n",
    "        ],\n",
    "        temperature=0.7,\n",
    "        stream=False\n",
    "    )\n",
    "\n",
    "    # 提取生成的 HTML 内容\n",
    "    if response.choices and len(response.choices) > 0:\n",
    "        html_content = response.choices[0].message.content\n",
    "        \n",
    "        # 保存到文件\n",
    "        with open(\"gomoku.html\", \"w\", encoding=\"utf-8\") as f:\n",
    "            f.write(html_content)\n",
    "        print(\"五子棋游戏已保存为 gomoku.html\")\n",
    "    else:\n",
    "        print(\"未收到有效响应\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"调用 API 出错: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4648730d-2ec3-494f-9546-52a7687d4589",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "deepseek",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
